const initialState = {
  allList: [] as ChannelType[],
  currentId: 1,
  userList: [] as ChannelType[],
};

export type ChannelType = {
  id: number;
  name: string;
};

export type ChannelActionType =
  | {
      type: 'channel/saveAllList';
      payload: ChannelType[];
    }
  | {
      type: 'channel/saveUserList';
      payload: ChannelType[];
    }
  | {
      type: 'channel/delUserList';
      payload: number;
    }
  | {
      type: 'channel/addUserList';
      payload: ChannelType;
    }
  | {
      type: 'channel/addCurrentId';
      payload: number;
    };
// eslint-disable-next-line import/no-anonymous-default-export
export default (
  state = initialState,
  action: ChannelActionType
): typeof initialState => {
  switch (action.type) {
    case 'channel/saveAllList':
      return {
        ...state,
        allList: [...action.payload],
      };
    case 'channel/saveUserList':
      return {
        ...state,
        userList: [...action.payload],
      };
    case 'channel/delUserList':
      return {
        ...state,
        userList: state.userList.filter((item) => item.id !== action.payload),
      };
    case 'channel/addUserList':
      return {
        ...state,
        userList: [...state.userList, action.payload],
      };
    case 'channel/addCurrentId':
      return {
        ...state,
        currentId: action.payload,
      };
    default:
      return state;
  }
};
